//
//  UIImage+ZK.m
//  ZAKER
//
//  Created by Anyfish002 on 16/2/16.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "UIImage+ZK.h"
#import "UIImage+Color.h"

@implementation UIImage(ZK)

+ (UIImage *)imageNameFromeBundle:(NSString *)imgName
{
    NSString *strBundle;
    if([ZKThemeManager shareInstance].themeMode == ZKThemeMode_Day) strBundle = @"theme_default.bundle";
    else strBundle = @"theme_dark.bundle";
    NSString *bundlePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:strBundle];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *img_path = [bundle pathForResource:imgName ofType:@"png"];
    return [UIImage imageWithContentsOfFile:img_path];
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


//转成黑白图像
+ (UIImage*)covertToGrayImageFromImage:(UIImage*)sourceImage
{
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    
    if (context == NULL) {
        return nil;
    }
    
    CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
    CGImageRef contextRef = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:contextRef];
    CGContextRelease(context);
    CGImageRelease(contextRef);
    
    return grayImage;
}

+ (UIImage *)imageWithColorString:(NSString *)colorString frame:(CGRect)frame
{
    return [self imageWithColor:[UIColor colorWithHexString:colorString] frame:frame];
}

+ (UIImage *)imageWithColor:(UIColor *)color frame:(CGRect)frame{
    CGRect rect = frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (UIImage *)croppedImage:(CGRect)bounds {
    UIImage *image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect(self.CGImage,bounds)];
    return image;
}


-(UIImage*)getCenterImage:(CGSize)originSize thumbSize:(CGSize)thumbSize{
    CGSize tempImageSize;
    UIImage *image=nil;
    //参照高的比例压缩图片
    if(originSize.width >= originSize.height){
        CGFloat scale=thumbSize.height/originSize.height;
        tempImageSize.height=thumbSize.height;
        tempImageSize.width=originSize.width*scale;
    }else{
        CGFloat scale=thumbSize.width/originSize.width;
        tempImageSize.width=thumbSize.width;
        tempImageSize.height=originSize.height*scale;
    }
    //原图的放大、缩小图，高宽比不变
    //    image = [self resizedImage:tempImageSize interpolationQuality:kCGInterpolationHigh];
    image = [self scaleToSize:tempImageSize];
    CGPoint point;
    if(originSize.height > originSize.width){
        point.y=( tempImageSize.height - thumbSize.height)*0.5;
        point.x=0;
    }else{
        point.x=( tempImageSize.width - thumbSize.width)*0.5;
        point.y=0;
    }
    CGRect rect=CGRectMake((int)point.x, (int)point.y, thumbSize.width, thumbSize.height);
    image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage,rect)];
    
    return image;
}

-(UIImage*)scaleToSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}


- (UIImage *)cutToCircle:(BOOL)clipToCircle
             withDiamter:(CGFloat)diameter
             borderColor:(UIColor *)borderColor
             borderWidth:(CGFloat)borderWidth
            shadowOffSet:(CGSize)shadowOffset
{
    if(diameter==0){
        diameter = self.size.height>self.size.width?self.size.width:self.size.height;
    }
    
    // increase given size for border and shadow    
    CGRect newRect = CGRectMake(0.0f,
                                0.0f,
                                diameter,
                                diameter);
    
    // fit image inside border and shadow
    CGRect imgRect = CGRectMake(borderWidth,
                                borderWidth,
                                newRect.size.width-2*borderWidth,
                                newRect.size.height-2*borderWidth);
    
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    // draw shadow
    if(!CGSizeEqualToSize(shadowOffset, CGSizeZero))
        CGContextSetShadowWithColor(context,
                                    CGSizeMake(shadowOffset.width, shadowOffset.height),
                                    3.0f,
                                    [UIColor colorWithWhite:0.0f alpha:0.45f].CGColor);
    
    // draw border
    // as circle or square
    
    CGPathRef borderPath = (clipToCircle) ? CGPathCreateWithEllipseInRect(imgRect, NULL) : CGPathCreateWithRect(imgRect, NULL);
    
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
    CGContextSetLineWidth(context, borderWidth);
    CGContextAddPath(context, borderPath);
    CGContextDrawPath(context, kCGPathFillStroke);
    CGPathRelease(borderPath);
    CGContextRestoreGState(context);
    
    // clip to circle
    UIBezierPath *imgPath = [UIBezierPath bezierPathWithOvalInRect:imgRect];
    [imgPath addClip];
    
    [self drawInRect:imgRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

-(UIImage*)getRecommedImage:(UIColor*)color{
    UIImage *image = [self imageWithTintColor:color];
    CGFloat margin = (image.size.width*2 - 286)/2;
    image = [image croppedImage:CGRectMake(margin, 0, image.size.width*2 - 2*margin,286)];
    return image;
}

@end
